package com.iran.Proxy.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 雕爷地代购公司
 */
public class DiaoCompany implements InvocationHandler {

    //被代理地对象
    private Object factory;

    public Object getFactory() {
        return factory;
    }

    public void setFactory(Object factory) {
        this.factory = factory;
    }

    /**
     * 通过Proxy动态获取动态代理地对象
     * @return
     */
    public Object getProxyInstance(){
        return Proxy.newProxyInstance(factory.getClass().getClassLoader(),factory.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        doSomeThingBefore();
        Object invoke = method.invoke(factory, args);
        doSomeThingAfter();
        return invoke;
    }
    private void doSomeThingBefore(){
        System.out.println("增强，做之前");
    }
    private void doSomeThingAfter(){
        System.out.println("增强，做之后");
    }
}
